package org.hhchat.others.toutiao.p2017;

import java.util.*;

/**
 * Created by wangzifan on 2017/3/24.
 */
public class toutiao04 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        HashMap<String,Integer>[] p = new HashMap[N];
        String[] line = new String[N];
        scanner.next();
        for (int i = 0; i < N; i++) {
            p[i] = new HashMap<>();
            line[i] = scanner.nextLine();
            String[] l = line[i].split("\\s");
            for (String str : l) {
                if(p[i].containsKey(str.toLowerCase())){
                    int t = p[i].get(str.toLowerCase());
                    p[i].put(str.toLowerCase(),t+1);
                } else {
                    p[i].put(str.toLowerCase(),1);
                }
            }
        }
        int [] count = new int [N];
        for(int i=0;i<M;i++){
            String query = scanner.nextLine();
            String [] l = query.split("\\s");
            for(int j=0;j<N;j++){
                for(String s : l){
                    if(p[j].containsKey(s.toLowerCase())){
                        count[j] += p[j].get(s.toLowerCase());
                    }
                }
            }

            System.out.println(line[Max(count)]);
        }
    }

    public static int Max(int [] c){
        int max = 0,index = -1;
        for(int i=0;i<c.length;i++) {
            if(max<c[i]){
                max=c[i];
                System.out.println(c[i]);
                index = i;
            }
        }
        return index;
    }

}